package com.stupro.cjlu.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stupro.cjlu.common.vo.StudentInfoVO;
import com.stupro.cjlu.common.vo.StudentItemVO;
import com.stupro.cjlu.entity.Students;
import com.stupro.cjlu.mapper.StudentsMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class StudentsDAO extends BaseDAO{
    public static final int PAGE_SIZE = 4;
    private final StudentsMapper mapper;

    public StudentsDAO(StudentsMapper mapper) {
        this.mapper = mapper;
    }

    public Students getByNumber(String stuUserNumber) {
        LambdaQueryWrapper<Students> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Students::getStuUserName, stuUserNumber);

        return mapper.selectOne(wrapper);
    }


    public int insert(Students students) {
        return mapper.insert(students);
    }

    public int delete(Integer stuID) {
        return mapper.deleteById(stuID);
    }

    public Students get(Integer stuID) {
        return mapper.selectById(stuID);
    }

    public int update(Students students) {
        return mapper.updateById(students);
    }

    public int count( String stuClass, String stuName) {
        return mapper.count( stuClass, stuName);
    }
//    废弃接口
//    public List<Students> getPage(Integer index, String stuClass, String stuName) {
//        Page<Students> page = new Page<>(index, PAGE_SIZE);
//        LambdaQueryWrapper<Students> wrapper = new LambdaQueryWrapper<>();
//        wrapper.like(StringUtils.isNotBlank(stuName),Students::getStuName,stuName)
//                .like(StringUtils.isNotBlank(stuClass),Students::getStuClass,stuClass);

//        return mapper.selectPage(page,wrapper).getRecords();
//    }

    public List<StudentItemVO> getPage(Integer index, String stuClass, String stuName){
        Page<StudentItemVO> page = new Page<>(index,PAGE_SIZE);
        stuClass = stuClass==null?"":stuClass;
        stuName = stuName==null?"":stuName;

        return mapper.getPage(page,stuClass,stuName).getRecords();
    }

    /*public Stirng countByClassId(String stuClass) {
        LambdaQueryWrapper<Students> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Students::getStuClass, stuClass);

        return mapper.selectCount(wrapper);
    }*/

    public List<Students> listName() {
        LambdaQueryWrapper<Students> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Students::getStuID, Students::getStuName);

        return mapper.selectList(wrapper);
    }

    /*public Integer getDepartmentIdById(Integer studentId) {
        return mapper.getDepartmentIdById(studentId);
    }*/

    /*public Integer getGradeById(Integer studentId) {
        return mapper.getGradeById(studentId);
    }*/

    public StudentInfoVO getStudentInfoById(Integer studentId) {
        return mapper.getStudentInfoById(studentId);
    }
}
